package org.blast.parser.main;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Logger;
import org.blast.parser.objects.BlastOutput;

public class XMLStringParser {

	private static Logger log = Logger.getLogger(XMLParser.class);
	private static final String DTO_PACKAGE_PATH = "org.blast.parser.objects";
	
	public BlastOutput parser(String xml) throws Throwable{
		
		BlastOutput blast_output = null;
		JAXBContext jaxbContext = null;
		Unmarshaller unmarshaller = null;
		
		StreamSource streamSource = null;
		StringReader stringReader = null;
		
		try{
			
			stringReader = new StringReader(xml);
			streamSource = new StreamSource(stringReader);
			
			jaxbContext = JAXBContext.newInstance( DTO_PACKAGE_PATH );
			unmarshaller = jaxbContext.createUnmarshaller();
			blast_output = (BlastOutput) unmarshaller.unmarshal( streamSource );
			
		} catch(Throwable t){
			
			System.out.println(t.toString());
			t.printStackTrace();
			log.error("Fail.", t);
			throw t;
			
		} 
		return blast_output;
	}
	
}
